<?php

use console\db\Migration;

/**
 * 系统通知数据表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m190102_180250_t_notice extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 系统通知表
         * @table `notice`
         */
        $this->tableBuilder([
            'name' => 'notice',
            'comment' => '系统通知表',
            'columns' => [
                'id' => $this->primaryKey(),
                'type_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('通知类型ID'),
                'sender_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('推送人ID'),
                'receiver_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('接收用户ID'),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('通知用户分组ID'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 通知消息内容表
         * @table `notice_content`
         */
        $this->tableBuilder([
            'name' => 'notice_content',
            'comment' => '通知消息内容表',
            'columns' => [
                'id' => $this->primaryKey(),
                'notice_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('通知ID'),
                'title' => $this->string(100)->notNull()->comment('消息标题'),
                'description' => $this->string(255)->notNull()->comment('消息描述'),
                'thumb' => $this->char(255)->defaultValue('')->comment('消息缩略图'),
                'type' => $this->char(20)->defaultValue('')->comment('链接类型，协议规定原生类型，空串标识为自身h5'),
                'data' => $this->string(255)->defaultValue('')->comment('数组转义字符串参数'),
                'url' => $this->string(255)->defaultValue('')->comment('直接外链地址'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 通知应用数据表
         * @table `notice_data`
         */
        $this->tableBuilder([
            'name' => 'notice_data',
            'comment' => '通知应用数据表',
            'columns' => [
                'id' => $this->primaryKey(),
                'notice_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('通知ID'),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'reply_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('回复ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('内容用户UID'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 通知分组表
         * @table `notice_group`
         */
        $this->tableBuilder([
            'name' => 'notice_group',
            'comment' => '通知分组表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('分组名称'),
                'method' => $this->char(20)->notNull()->comment('程序实现方法名'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'method',
                    'unique' => true,
                ]
            ]
        ]);

        /**
         * 通知分组关联标签表
         * @table `notice_group_tag`
         */
        $this->tableBuilder([
            'name' => 'notice_group_tag',
            'comment' => '通知分组关联标签表',
            'columns' => [
                'id' => $this->primaryKey(),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('分组ID'),
                'tag_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户标签ID'),
            ]
        ]);

        /**
         * 通知发送人表
         * @table `notice_sender`
         */
        $this->tableBuilder([
            'name' => 'notice_sender',
            'comment' => '通知发送人表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(20)->notNull()->comment('用户名'),
                'avatar' => $this->char(255)->defaultValue('')->comment('头像'),
                'admin_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('关联管理用户ID'),
                'sync_time' => $this->unixTimestamp()->comment('最后同步时间'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 通知类型表
         * @table `notice_type`
         */
        $this->tableBuilder([
            'name' => 'notice_type',
            'comment' => '通知类型表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->char(20)->notNull()->comment('名称'),
                'label' => $this->string(50)->notNull()->comment('类型名称'),
                'icon' => $this->char(255)->defaultValue('')->comment('类型图标'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'name',
                    'unique' => true,
                ]
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%notice_type}}');
        $this->dropTable('{{%notice_sender}}');
        $this->dropTable('{{%notice_group_tag}}');
        $this->dropTable('{{%notice_group}}');
        $this->dropTable('{{%notice_data}}');
        $this->dropTable('{{%notice_content}}');
        $this->dropTable('{{%notice}}');
    }

}
